﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Assets.Scripts;

public class CustomManager : MonoBehaviour
{

    public GameObject ContentRoot;


    public int chooseDifficulty = -1;
    public GameObject CustomPrefabs;

    public Sprite startLightSprite;
    public Sprite startGreySprite;
    public Sprite customItemLightSprite;
    public Sprite customItemGreySprite;


    [SerializeField] private Dictionary<int, CustomsInfo> CustomList = new Dictionary<int, CustomsInfo>();


    private void Awake()
    {
        //测试数据
        for (int i = 0; i < 4; i++)
        {
            CustomsInfo temp = new CustomsInfo();
            temp.currentMaxPass = Random.Range(1, 10);
            temp.customInfos = new List<CustomInfo>();
            CustomList[i] = temp;

            for (int j = 0, k = Random.Range(10, 20); j < k; j++)
            {
                CustomInfo info = new CustomInfo();
                info.index = j;
                if (j < temp.currentMaxPass)
                {
                    info.star = Random.Range(1, 3);
                    temp.customInfos.Add(info);
                }
                else
                {
                    info.star = 0;
                    temp.customInfos.Add(info);
                }
            }
        }
    }


    // Start is called before the first frame update
    void Start()
    {


    }

    public void onClickCustom( int tag )
    {
        Debug.Log("choose" + tag);
    }

    public void OnEnable()
    {
        if (chooseDifficulty == -1) return;

        Debug.Log("chooseDifficulty" + chooseDifficulty);

        int childNum = ContentRoot.transform.childCount;
        Transform childrenRoot = ContentRoot.transform;


        //可以使用的数量
        CustomsInfo current = CustomList[chooseDifficulty];
        //可使用关卡数量
        int useCount = current.customInfos.Count;
        CustomInfo temp;

        
        //关卡星级
        for (int i = 0; i < useCount; i++)
        {
            temp = current.customInfos[i];
           
            int star = temp.star;

            int tag = i;
            //复用
            if (i < childNum)
            {
                //背景图
                if (i < current.currentMaxPass)
                {
                    childrenRoot.GetChild(i).GetComponent<Image>().sprite = customItemLightSprite;
                }
                else
                {
                    childrenRoot.GetChild(i).GetComponent<Image>().sprite = customItemGreySprite;
                }

                childrenRoot.GetChild(i).GetComponent<Button>().onClick.RemoveAllListeners();
                childrenRoot.GetChild(i).GetComponent<Button>().onClick.AddListener( ( ) => onClickCustom( tag ) );

                Transform starImg;
                //星星
                for (int j = 0; j < 3; j++)
                {

                    starImg = childrenRoot.GetChild(i).Find("Image" + j);
                    if (j < star)
                    {
                        starImg.GetComponent<Image>().sprite = startLightSprite;
                    }
                    else
                    {
                        starImg.GetComponent<Image>().sprite = startGreySprite;
                    }

                    //关卡
                    childrenRoot.GetChild(i).Find("Tab").GetComponent<Text>().text = i.ToString();
                }
            }//实例化
            else
            {

                Transform item = Instantiate(CustomPrefabs, ContentRoot.transform).transform;
                //背景图
                if (i < current.currentMaxPass)
                {
                    item.GetComponent<Image>().sprite = customItemLightSprite;
                }
                else
                {
                    item.GetComponent<Image>().sprite = customItemGreySprite;
                }

                //事件
                item.GetComponent<Button>( ).onClick.AddListener( ( ) => onClickCustom( tag ) );

                //关卡
                item.Find("Tab").GetComponent<Text>().text = i.ToString();

                Transform starImg;
                //星星
                for (int j = 0; j < 3; j++)
                {
                    //复用
                    starImg = item.Find("Image" + j);
                    if (j < star)
                    {
                        starImg.GetComponent<Image>().sprite = startLightSprite;
                    }
                    else
                    {
                        starImg.GetComponent<Image>().sprite = startGreySprite;
                    }
                }
            }

        }

        //干掉多余的
        if (childNum > useCount)
        {
            for (int i = useCount; i < childNum; i++)
            {
                Transform child = childrenRoot.GetChild(i);
                Destroy(child.gameObject);
            }
        }

    }



    // Update is called once per frame
    void Update()
    {

    }
}
